package com.prizedraw.prize_draw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prizedraw.prize_draw.entity.DepartmentRole;
import com.prizedraw.prize_draw.entity.UserDepartmentRole;
import com.prizedraw.prize_draw.mapper.DepartmentRoleMapper;
import com.prizedraw.prize_draw.mapper.UserDepartmentRoleMapper;
import com.prizedraw.prize_draw.service.DepartmentRoleService;
import com.prizedraw.prize_draw.service.DepartmentService;
import com.prizedraw.prize_draw.service.SysRoleService;
import com.prizedraw.prize_draw.service.UserDepartmentRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czj666
 * @since 2021-09-07
 */
@Service
public class DepartmentRoleServiceImpl extends ServiceImpl<DepartmentRoleMapper, DepartmentRole> implements DepartmentRoleService {

    @Autowired
    DepartmentService departmentService;

    @Autowired
    SysRoleService sysRoleService;

    @Override
    public List<DepartmentRole> listDepartmentRoleByUserId(Integer userId) {
        // 找到用户所有的部门职位id
        List<DepartmentRole> drs = this.list(new QueryWrapper<DepartmentRole>().inSql("id", "select department_role_id from user_department_role where user_id = " + userId));
        // 根据部门职位id进行遍历查找对应对象
        drs.stream().forEach(dr -> {
            dr.setDepartment(departmentService.getById(dr.getDepartmentId()));
            dr.setSysRole(sysRoleService.getById(dr.getRoleId()));
        });
        return drs;
    }
}
